package com.mobi.Survey.adapters;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mobi.Survey.lib.activities.AbstractScreen;
import com.mobi.Survey.lib.activities.Messages;
import com.mobi.Survey.lib.activities.SurveyDownloadTask;
import com.mobi.Survey.lib.activities.SurveyDownloadTask.SurveyDownloadCallback;
import com.mobi.Survey.lib.activities.SurveyListScreen;
import com.mobi.Survey.lib.model.Credentials;
import com.mobi.Survey.lib.model.FavoritePojo;
import com.mobi.Survey.lib.utils.Helper;
import com.mobi.Survey.map.MapLogin;
import com.mobi.Survey.map.R;

public class CustomAdapterFavorites extends BaseAdapter {
	private List<FavoritePojo> data = new ArrayList<FavoritePojo>();
	protected LayoutInflater mInflater;
	private AbstractScreen screen;

	public CustomAdapterFavorites(AbstractScreen ctx) {
		this.screen = ctx;
		this.mInflater = LayoutInflater.from(ctx);
	}

	public void setData(List<FavoritePojo> data) {
		this.data = data;
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public FavoritePojo getItem(int position) {

		return data.get(position);
	}

	@Override
	public long getItemId(int arg0) {

		return 0;
	}

	class ViewHolder {

		TextView tvDescription;
		TextView tvUserId;
		View btnAutoLoad;
		View btnMakeFavorite;
		View ivSurveyIcon;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {

		final ViewHolder holder;
		final FavoritePojo favorite = data.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.favorite_list_row, null);
			holder = new ViewHolder();

			holder.tvDescription = (TextView) convertView
					.findViewById(R.id.tvDescription);

			holder.tvUserId = (TextView) convertView
					.findViewById(R.id.tvUserId);

			holder.btnAutoLoad = convertView.findViewById(R.id.btnAutoLoad);
			holder.btnMakeFavorite = convertView
					.findViewById(R.id.btnMakeFavorite);
			holder.ivSurveyIcon = convertView.findViewById(R.id.ivSurveyIcon);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		holder.btnMakeFavorite.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				onFavoriteClick(favorite, v);

			}

		});

		holder.tvDescription.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onIconClick(favorite, v);

			}
		});

		holder.tvUserId.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onIconClick(favorite, v);

			}
		});
		holder.ivSurveyIcon.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {
				onIconClick(favorite, v);
			}
		});
		holder.btnAutoLoad.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onAutoLoadClick(favorite, v);

			}
		});

		if (favorite.isFavorite()) {
			holder.btnMakeFavorite.setBackgroundDrawable(holder.btnMakeFavorite
					.getResources().getDrawable(R.drawable.favorite_active));

		} else {

			holder.btnMakeFavorite.setBackgroundDrawable(holder.btnMakeFavorite
					.getResources().getDrawable(R.drawable.favorite_inactive));
		}

		// Is Autoload and user id is saved ?

		String userId = favorite.getUserId();
		String des = favorite.getDescription();
		if (AbstractScreen.isAutoLoadId(screen)
				&& AbstractScreen.getAutoLoadUserId(this.screen)
						.equalsIgnoreCase(favorite.getUserId())) {
			favorite.setAutoLoad(true);
			holder.btnAutoLoad.setBackgroundDrawable(holder.btnAutoLoad
					.getResources().getDrawable(R.drawable.autoload_active));

		} else {
			favorite.setAutoLoad(false);
			holder.btnAutoLoad.setBackgroundDrawable(holder.btnAutoLoad
					.getResources().getDrawable(R.drawable.autoload_inactive));
		}

		if (null != userId) {
			holder.tvUserId.setText(userId.trim());
		}
		if (null != des) {
			holder.tvDescription.setText(des.trim());
		}

		String imageUrl = favorite.getIconUrl();

		if (null != imageUrl && imageUrl.length() > 0) {

			String outputfile = Helper.getTempFile(Helper
					.getHashNameFromUrl(imageUrl) + "");
			if (new File(outputfile).exists()) {

				boolean contain = MapLogin.getUserListIcons().containsKey(
						imageUrl);
				if (contain) {
					Bitmap b = MapLogin.getUserListIcons().get(imageUrl);
					holder.ivSurveyIcon.setBackgroundDrawable(null);
					holder.ivSurveyIcon
							.setBackgroundDrawable(new BitmapDrawable(b));
				} else {
					Bitmap b = Helper.decode70x70(outputfile, 3);
					MapLogin.getUserListIcons().put(imageUrl, b);
					holder.ivSurveyIcon.setBackgroundDrawable(null);
					if (null != b)
						holder.ivSurveyIcon
								.setBackgroundDrawable(new BitmapDrawable(b));
					// favorite.setIconBitmap(b);
				}
			} else {
				holder.ivSurveyIcon.setBackgroundDrawable(null);
				holder.ivSurveyIcon.setBackgroundDrawable(convertView
						.getResources().getDrawable(R.drawable.ic_launcher));
			}
		} else {
			holder.ivSurveyIcon.setBackgroundDrawable(null);
			holder.ivSurveyIcon.setBackgroundDrawable(convertView
					.getResources().getDrawable(R.drawable.ic_launcher));

		}

		return convertView;
	}

	private void onFavoriteClick(FavoritePojo favorite, View v) {

		favorite.setFavorite(!favorite.isFavorite());
		if (favorite.isFavorite()) {
			v.setBackgroundDrawable(v.getResources().getDrawable(
					R.drawable.favorite_active));

			FavoritePojo.commit(v.getContext(), favorite);

		} else {

			v.setBackgroundDrawable(v.getResources().getDrawable(
					R.drawable.favorite_inactive));

			FavoritePojo.deleteBySurveyId(v.getContext(),
					favorite.getSurveyId());
		}

	}

	private void onIconClick(final FavoritePojo favorite, final View v) {

		AbstractScreen.setMapTabId(v.getContext(), 0);

		Credentials c = new Credentials(favorite.getUserId(), "");
		SurveyDownloadTask task = new com.mobi.Survey.lib.activities.SurveyDownloadTask(
				v.getContext(), c);
		task.setCallback(new SurveyDownloadCallback() {

			@Override
			public void onPostExecute(int result) {
				switch (result) {
				case SurveyDownloadTask.SWITCH_SCREEN:
					screen.setUserPrefrences();
					Intent i = new Intent(v.getContext(),
							SurveyListScreen.class);
					i.putExtra("user_id", favorite.getUserId());
					v.getContext().startActivity(i);
					break;

				case SurveyDownloadTask.NO_INTERNET_CONNECTION:
					screen.showShortToast(Messages.NO_INTERNET);
					break;
				}

			}

			@Override
			public void inBackground() {
				screen.populateUserBitmaps(true);
			}
		});

		task.execute();

	}

	private void onAutoLoadClick(FavoritePojo favorite, View v) {

		favorite.setAutoLoad(!favorite.isAutoLoad());
		if (favorite.isAutoLoad()) {
			v.setBackgroundDrawable(v.getResources().getDrawable(
					R.drawable.autoload_active));

			AbstractScreen.setAutoLoadUserId(favorite.getUserId(), this.screen);

			AbstractScreen.setAutoLoad(true, v.getContext());
			notifyDataSetChanged();

		} else {

			v.setBackgroundDrawable(v.getResources().getDrawable(
					R.drawable.autoload_inactive));
			AbstractScreen.setAutoLoadUserId("", this.screen);

			AbstractScreen.setAutoLoad(false, v.getContext());
			notifyDataSetChanged();
		}

	}
}
